package edu.umn.contactviewer;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import edu.umn.contactviewer.datastore.ContactListDataService;
import edu.umn.contactviewer.datastore.impl.ContactListDataServiceRESTWebServiceImpl;

/**
 * Displays a list of contacts.
 * 
 */
public class ContactListActivity extends ListActivity {

	private final ContactListDataService contactListDataStore;
	private final ArrayList<Contact> contactListData;
	private ArrayAdapter<Contact> contactListAdapter;
	static final int CREATE_CONTACT = 0;
	static final int CONTACT_DETAILS = 1;
	static final int DELETE_CONTACTS = 2;

	// Constructor
	public ContactListActivity() {
		//contactListDataStore = new ContactListDataServiceDatabaseImpl(this);
		contactListDataStore = new ContactListDataServiceRESTWebServiceImpl(this);
		contactListData = new ArrayList<Contact>();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		new ContactsToolbar();

		// initialize the list view
		contactListAdapter = new ContactAdapter(this, R.layout.list_item,
				contactListData);

		// retrieve initial set of Contact list data
		updateContactListData();

		setListAdapter(contactListAdapter);
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		// handle the item click events
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// retrieve the clicked contact
				Contact clickedContact = ((ContactAdapter) getListAdapter())
						.getItem(position);

				// create the details intent
				Intent detailsIntent = new Intent(ContactListActivity.this,
						DetailsActivity.class);

				// add the contact to the details intent
				detailsIntent.putExtra("contactForDetails", clickedContact);

				// start the details activity
				startActivityForResult(detailsIntent, CONTACT_DETAILS);
			}
		});
	}

	private void updateContactListData() {
		// clear the current data
		contactListData.clear();

		// retrieve fresh data from the datastore
		contactListData.addAll(contactListDataStore.getContactList());

		// notify the view to refresh itself
		contactListAdapter.notifyDataSetChanged();
	}

	/*
	 * We need to provide a custom adapter in order to use a custom list item
	 * view.
	 */
	public class ContactAdapter extends ArrayAdapter<Contact> {

		public ContactAdapter(Context context, int textViewResourceId,
				List<Contact> objects) {
			super(context, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = getLayoutInflater();
			View item = inflater.inflate(R.layout.list_item, parent, false);

			Contact contact = getItem(position);
			((TextView) item.findViewById(R.id.item_name)).setText(contact
					.getName());
			((TextView) item.findViewById(R.id.item_title)).setText(contact
					.getTitle());
			((TextView) item.findViewById(R.id.item_phone)).setText(contact
					.getPhone());

			return item;
		}
	}

	private class ContactsToolbar extends ToolbarConfig {
		public ContactsToolbar() {
			super(ContactListActivity.this, getString(R.string.contacts));
			// initialize the create button and add action listener
			Button rightButton = getToolbarRightButton();
			rightButton.setText(getString(R.string.create));
			rightButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// create the create intent
					Intent createIntent = new Intent(ContactListActivity.this,
							CreateActivity.class);

					// start the create activity
					startActivityForResult(createIntent, CREATE_CONTACT);
				}
			});
			rightButton.setVisibility(View.VISIBLE);
		}
	}

	@Override
	// for handling returns from create
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if ((CREATE_CONTACT == requestCode || CONTACT_DETAILS == requestCode || DELETE_CONTACTS == requestCode)
				&& RESULT_OK == resultCode) {
			// retrieve fresh data to pull new contact from the datastore
			updateContactListData();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.contact_list_options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (R.id.delete_options_menu_button == item.getItemId()) {
			// create the delete intent
			Intent deleteIntent = new Intent(ContactListActivity.this,
					DeleteActivity.class);

			// start the delete activity
			startActivityForResult(deleteIntent, DELETE_CONTACTS);
		}	

		return true;
	}
}
